package org.jsbd.boss.action;

import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ConstantsCmp;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.dao.IBrushConfigDao;
import org.jsbd.boss.dao.IChannelApkInfoDAO;
import org.jsbd.boss.domian.ChannelApkConfigBaseModel;
import org.jsbd.boss.domian.ChannelApkConfigTimeModel;
import org.jsbd.boss.domian.channel.BrushConfig;
import org.jsbd.boss.service.IChannelLimitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 留存日志查询controller
 *
 * @author <a href="mailto:zhangbinghua@zhexinit.com" >zbh</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/channelLimitModel")
public class ChannelLimitModelController {

    private Logger LOGGER = LoggerFactory.getLogger(ChannelLimitModelController.class);

    @Autowired
    private IChannelLimitService channelLimitService;

    @Autowired
    private IChannelApkInfoDAO channelApkInfoDAO;

    @Resource
    private IBrushConfigDao brushConfigDao;


    /**
     * getChannelApkConfigTimeModellist:
     *
     * @param request
     * @param resp
     */
    @RequestMapping(value = "/addmodel", params = "json")
    @ResponseBody
    public Map<String, Object> getChannelApkConfigTimeModellist(HttpServletRequest request, HttpServletResponse resp, ChannelApkConfigTimeModel channelApkConfigTimeModel, Model model) {
        String mark = channelApkConfigTimeModel.getMark();
        List<ChannelApkConfigTimeModel> channelApkConfigTimeModellist = new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>();
        //拉活id
        if (StringUtils.isNotBlank(mark) && mark.length() < 8) {
            BrushConfig brushConfig = new BrushConfig();
            brushConfig.setId(Integer.parseInt(mark));
            BrushConfig brushConfig1 = brushConfigDao.selectConfig(brushConfig);
            if (brushConfig1 == null || brushConfig1.getDayLimit() == null) {
                return map;
            }
            channelLimitService.getTimeModelByMark(mark, brushConfig1.getDayLimit());
        }
        channelApkConfigTimeModellist = channelLimitService.getChannelApkConfigTimeModelList(channelApkConfigTimeModel.getMark(), ConstantsCmp.add);
        map.put("rows", channelApkConfigTimeModellist);
        return map;
    }

    @RequestMapping(value = "/basemodels", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> baseModellist(HttpServletResponse resp, ChannelApkConfigBaseModel baseModel, Model model) {
        List<ChannelApkConfigBaseModel> channelApkConfigTimeModellist = channelLimitService.selectChannelApkConfigModel(baseModel);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", channelApkConfigTimeModellist);
        return map;
    }

    @RequestMapping(value = "/addmodel", method = RequestMethod.GET)
    public String configList(Model model) {
        return "channelapk/addmodelconfig";
    }

    // 基本配置查询展示接口
    @RequestMapping(value = "/addmodelUpdate")
    @ResponseBody
    public void provinceRateUpdate(HttpServletRequest request, HttpServletResponse resp, ChannelApkConfigTimeModel form) {
        channelLimitService.saveLimitModelBymark(form);
        ResponseUtils.responseSuccess(resp);
    }

    // 基本配置查询展示接口
    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ResponseBody
    public void batchUpdate(HttpServletResponse resp, @RequestBody ChannelApkConfigTimeModel[] configs) {
        List<ChannelApkConfigTimeModel> list = Arrays.asList(configs);
        channelLimitService.saveLimitModelList(list, 0);
        ResponseUtils.responseSuccess(resp);
    }

    // 基本配置查询展示接口
    @RequestMapping(value = "/retentBatchUpdate", method = RequestMethod.POST)
    @ResponseBody
    public void retentBatchUpdate(HttpServletResponse resp, @RequestBody ChannelApkConfigTimeModel[] configs) {
        List<ChannelApkConfigTimeModel> list = Arrays.asList(configs);
        channelLimitService.saveLimitModelList(list, 1);
        ResponseUtils.responseSuccess(resp);
    }

    /**
     * getChannelApkConfigTimeModellist:
     *
     * @param request
     * @param resp
     */
    @RequestMapping(value = "/retentmodel", params = "json")
    @ResponseBody
    public Map<String, Object> getChannelApkConfigTimeRetentModellist(HttpServletRequest request, HttpServletResponse resp, ChannelApkConfigTimeModel channelApkConfigTimeModel, Model model) {
        List<ChannelApkConfigTimeModel> channelApkConfigTimeModellist = channelLimitService.getChannelApkConfigTimeModelList(channelApkConfigTimeModel.getMark(), ConstantsCmp.retent);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", channelApkConfigTimeModellist);
        return map;
    }

    @RequestMapping(value = "/retentmodel", method = RequestMethod.GET)
    public String retentconfigList(Model model) {
        return "channelapk/retentmodelconfig";
    }

    // 基本配置查询展示接口
    @RequestMapping(value = "/retentmodelUpdate")
    @ResponseBody
    public void retentUpdate(HttpServletRequest request, HttpServletResponse resp, ChannelApkConfigTimeModel form) {
        channelLimitService.saveLimitModelBymark(form);
        ResponseUtils.responseSuccess(resp);
    }

}
